﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2009 ThinkWide
+-------------------------------------------------------------
项目: 序列化类


版本: 1


创建日期:2010-8-3


作者: 章称

+-------------------------------------------------------------
描述:

更新:   2010-8-3

ToDo:

***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace MF.Core
{
    public class MFSerializer
    {

        public static Stream WriteToStream<T>(T value)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();

            serializer.Serialize(ms, value);
            return ms;

        }

        public static T StreamToObject<T>(Stream value)
        {
            T instance = Activator.CreateInstance<T>();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            value.Seek(0, SeekOrigin.Begin);
            instance = (T)serializer.Deserialize(value);
            return instance;


        }

        public static string WriteToString<T>(T value)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            
            using (MemoryStream writer = new MemoryStream())
            {
                serializer.Serialize(writer, value);
                string  str= Encoding.Default.GetString(writer.ToArray()); 
                writer.Close();
                return str;
            }

            
        }

        public static T StringToObject<T>(string value)
        {
            T instance = Activator.CreateInstance<T>();
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (MemoryStream reader = new MemoryStream(System.Text.Encoding.Default.GetBytes(value)))
            {
                
                instance = (T)serializer.Deserialize(reader);
                reader.Close();
                return instance;
            } 

        }

        public static bool WriteToFile<T>(T value,string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (TextWriter writer = new StreamWriter(filePath))
            {
                serializer.Serialize(writer, value);
                writer.Close();
            }


            return true;

        }

        public static T ReadFromFile<T>(string filePath)
        {
            T instance=Activator.CreateInstance<T>();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (TextReader reader = new StreamReader(filePath))
            {
                instance=(T)serializer.Deserialize(reader);
                reader.Close();
            }
            return instance;
        }
        
    }
}
